package com.scpii.universal.util;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;

import com.scpii.universal.constant.GenericConstants;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhCacheManager {
	private static CacheManager cacheManager;
	private static Cache cache;
	private static Log log = LogFactory.getLog(EhCacheManager.class);
	private static final String ERROR = "初始化EhCacheManager错误";
	
	static {
		try {
			cacheManager = CacheManager.create(new ClassPathResource("ehcache.xml").getInputStream());
		} catch (CacheException e) {
			log.error(ERROR, e);
		} catch (IOException e) {
			log.error(ERROR, e);
		}
		cache = cacheManager.getCache("propertiesCache");
	}

	public void removeProduceSysCache() {
		cache.removeAll();
	}
	
	public static void addObject(String key,Object value) {
		Element ele = new Element(key, value);
		cache.put(ele);
	}
	
	public static Object getObject(String key) {
		if (cache.get(key) == null)
			return ResourceBundle.getBundle(
					GenericConstants.GLOBAL_PROPERTIES_PATH,
					Locale.getDefault()).getString(key);
		return cache.get(key).getValue();
	}
	
	public static List<?> getAllObjects() {
		return cache.getKeys();
	}
}
